<?
require 'global.php';

if($customer_id==0) { header('Location:login.html'); exit; }

$gee=$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);
$refund_reason = new DB(Trefund_reason, $conn);
$refund_info = new DB(Trefund, $conn);

$url='myorder';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 备用字段
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$code=isset($_GET["code"])?clean($_GET["code"]):'none';
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"])) $page=$_GET["page"];

// 此处应验证订单是否为当前用户所有
if($do=='cancel'){
	if($id==0) { header('Location:myorder.php'); exit; }
	$arr=array(
		'order_status'=>'7',
		);
	if($gee->update($arr,'id',$id)) {
		$order=$gee->get_row($id);
		$ref_no=$order['ref_no'];
		// 恢复库存 restore stock
		$product->restore_stock_by_order($ref_no);
		// 恢复积分
		
		wheel('myorder.php?page='.$page); exit; 
	}
}

if($do=='delivery'){
	if($code=='none') { header('Location:myorder.php'); exit; }
	// 验证所有权
	$tem = $gee->get_row($code,'ref_no');
	if(is_null($tem) || $tem['customer_id']!=$customer_id) { header('Location:myorder.php'); exit; }
	$arr=array(
		'order_status'=>'5',
		);
	if($gee->update($arr,'ref_no',$code)) { wheel('myorder.php?page='.$page.'#t'.$code); exit; }
}

$gee->rows=10;
$where=array();
$where[]=array('name'=>'customer_id','val'=>$customer_id);
$order_by='id';
$pages=$gee->count_pages($where);
$tem=$gee->get_page($page,$where,$order_by);

$info=array();
$in_array=array();
foreach($tem as $val){ $info[$val['id']]=$val; $in_array[]=$val['id']; }

$where_detail=array();
$where_detail[]=array('name'=>'sales_order_id','val'=>$in_array,'opn'=>'in');
$tem=$sales_order_detail->get_rows_advanced(10240,$where_detail);
foreach($tem as $val){
	$info[$val['sales_order_id']]['items'][$val['id']]=$val;
}

// payment method 支付方式
$arr_payment=array();
$tem=$payment_method->get_rows(32,'is_enabled',1,'seq_no');
foreach ($tem as $val){
	$arr_payment[$val['id']]=$val;
}

// 最终结果
// pre($info);

// seo
$cate_name=WEB_MY_ORDER;
$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
$kwd=$title;
$dpn=$kwd;



/*订单取消原因*/
$refund = $refund_reason->get_rows(1024, 'type', 2, 'seq_no');
$refund_arr = $code_array['refund_status'];

// view 加载视图
require VIEW.$url.'.php';

// end of file
